#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qcustomplot.h"
#include "dataclass.h"
#include <QFileDialog>
#include <QDebug>


DataClass *dataC = new DataClass;
QVector<double> xx(7882);
QVector<double> ymax0(7882), ymax1(7882), ymax2(7882), ymax3(7882), ymax4(7882), ymax5(7882);
QVector<double> ymax6(7882), ymax7(7882), ymax8(7882), ymax9(7882), ymaxA(7882), ymaxB(7882);


QVector<double> ymin(7882);
float minX,maxX,minY,maxY;

bool flag[12];


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QCustomPlot *customPlot = ui->widget;//customPlot指针指向控件

    //下面两行代码设置图例可见，并设置风格
    customPlot->legend->setVisible(true);
    customPlot->legend->setFont(QFont("Helvetica",9));

    ui->actionWave0->setChecked(true);
    ui->actionWave1->setChecked(true);
    ui->actionWave2->setChecked(true);
    ui->actionWave3->setChecked(true);
    ui->actionWave4->setChecked(true);
    ui->actionWave5->setChecked(true);
    ui->actionWave6->setChecked(true);
    ui->actionWave7->setChecked(true);
    ui->actionWave8->setChecked(true);
    ui->actionWave9->setChecked(true);
    ui->actionWaveA->setChecked(true);
    ui->actionWaveB->setChecked(true);

    checkWave0();
    checkWave1();
    checkWave2();
    checkWave3();
    checkWave4();
    checkWave5();
    checkWave6();
    checkWave7();
    checkWave8();
    checkWave9();
    checkWaveA();
    checkWaveB();



    //下面代码的功能：
    //连接Wave按钮和操作：如果选择，则正常绘制数据，如果不选择，则置零该通道数据
    connect(ui->actionWave0, &QAction::triggered, this, [=](){
        checkWave0();
        findPlot();
    });
    connect(ui->actionWave1, &QAction::triggered, this, [=](){
        checkWave1();
        findPlot();
    });
    connect(ui->actionWave2, &QAction::triggered, this, [=](){
        checkWave2();
        findPlot();
    });
    connect(ui->actionWave3, &QAction::triggered, this, [=](){
        checkWave3();
        findPlot();
    });
    connect(ui->actionWave4, &QAction::triggered, this, [=](){
        checkWave4();
        findPlot();
    });
    connect(ui->actionWave5, &QAction::triggered, this, [=](){
        checkWave5();
        findPlot();
    });
    connect(ui->actionWave6, &QAction::triggered, this, [=](){
        checkWave6();
        findPlot();
    });
    connect(ui->actionWave7, &QAction::triggered, this, [=](){
        checkWave7();
        findPlot();
    });
    connect(ui->actionWave8, &QAction::triggered, this, [=](){
        checkWave8();
        findPlot();
    });
    connect(ui->actionWave9, &QAction::triggered, this, [=](){
        checkWave9();
        findPlot();
    });
    connect(ui->actionWaveA, &QAction::triggered, this, [=](){
        checkWaveA();
        findPlot();
    });
    connect(ui->actionWaveB, &QAction::triggered, this, [=](){
        checkWaveB();
        findPlot();
    });

    connect(ui->actionWaveform, &QAction::triggered, this, &MainWindow::waveForm);
    connect(ui->actionHome, &QAction::triggered, this, &MainWindow::homeWaveForm);

    customPlot->setParent(this);

    //下面代码的功能：
    //添加图线，并设置颜色
    customPlot->addGraph();
    customPlot->graph(0)->setPen(QPen(Qt::blue));

    customPlot->addGraph();
    customPlot->graph(1)->setPen(QPen(Qt::red));

    customPlot->addGraph();
    customPlot->graph(2)->setPen(QPen(Qt::yellow));

    customPlot->addGraph();
    customPlot->graph(3)->setPen(QPen(Qt::green));

    customPlot->addGraph();
    customPlot->graph(4)->setPen(QPen(Qt::black));

    customPlot->addGraph();
    customPlot->graph(5)->setPen(QPen(Qt::magenta));

    customPlot->addGraph();
    customPlot->graph(6)->setPen(QPen(Qt::lightGray));

    customPlot->addGraph();
    customPlot->graph(7)->setPen(QPen(Qt::darkYellow));

    customPlot->addGraph();
    customPlot->graph(8)->setPen(QPen(Qt::darkRed));

    customPlot->addGraph();
    customPlot->graph(9)->setPen(QPen(Qt::darkCyan));

    customPlot->addGraph();
    customPlot->graph(10)->setPen(QPen(Qt::cyan));

    customPlot->addGraph();
    customPlot->graph(11)->setPen(QPen(Qt::darkBlue));

    //下面12行代码设置图例中的graph名称
    customPlot->graph(0)->setName("通道0");
    customPlot->graph(1)->setName("通道1");
    customPlot->graph(2)->setName("通道2");
    customPlot->graph(3)->setName("通道3");
    customPlot->graph(4)->setName("通道4");
    customPlot->graph(5)->setName("通道5");
    customPlot->graph(6)->setName("通道6");
    customPlot->graph(7)->setName("通道7");
    customPlot->graph(8)->setName("通道8");
    customPlot->graph(9)->setName("通道9");
    customPlot->graph(10)->setName("通道10");
    customPlot->graph(11)->setName("通道11");


    //下面4行代码设置customPlot更好看
    customPlot->xAxis2->setVisible(true);
    customPlot->xAxis2->setTickLabels(false);
    customPlot->yAxis2->setVisible(true);
    customPlot->yAxis2->setTickLabels(false);

    /*通过下面这句语句，可以屏蔽掉QCustomPlot的鼠标消息*/
    customPlot->setAttribute(Qt::WA_TransparentForMouseEvents, true);

}

void MainWindow::checkWave0(){
    if(ui->actionWave0->isChecked()){
        flag[0] = true;
    }else{
        flag[0] = false;
    }
}
void MainWindow::checkWave1(){
    if(ui->actionWave1->isChecked()){
        flag[1] = true;
    }else{
        flag[1] = false;
    }
}
void MainWindow::checkWave2(){
    if(ui->actionWave2->isChecked()){
        flag[2] = true;
    }else{
        flag[2] = false;
    }
}
void MainWindow::checkWave3(){
    if(ui->actionWave3->isChecked()){
        flag[3] = true;
    }else{
        flag[3] = false;
    }
}
void MainWindow::checkWave4(){
    if(ui->actionWave4->isChecked()){
        flag[4] = true;
    }else{
        flag[4] = false;
    }
}
void MainWindow::checkWave5(){
    if(ui->actionWave5->isChecked()){
        flag[5] = true;
    }else{
        flag[5] = false;
    }
}
void MainWindow::checkWave6(){
    if(ui->actionWave6->isChecked()){
        flag[6] = true;
    }else{
        flag[6] = false;
    }
}
void MainWindow::checkWave7(){
    if(ui->actionWave7->isChecked()){
        flag[7] = true;
    }else{
        flag[7] = false;
    }
}
void MainWindow::checkWave8(){
    if(ui->actionWave8->isChecked()){
        flag[8] = true;
    }else{
        flag[8] = false;
    }
}
void MainWindow::checkWave9(){
    if(ui->actionWave9->isChecked()){
        flag[9] = true;
    }else{
        flag[9] = false;
    }
}
void MainWindow::checkWaveA(){
    if(ui->actionWaveA->isChecked()){
        flag[10] = true;
    }else{
        flag[10] = false;
    }
}
void MainWindow::checkWaveB(){
    if(ui->actionWaveB->isChecked()){
        flag[11] = true;
    }else{
        flag[11] = false;
    }
}

void MainWindow::mD2QVector(){
    for (int i=0; i<7882; ++i){
        xx[i] = dataC->indData[i];
        ymax0[i] = dataC->maxData0[i];
        //ymin[i] = dataC->minData1[i];
        ymax1[i] = dataC->maxData1[i];
        ymax2[i] = dataC->maxData2[i];
        ymax3[i] = dataC->maxData3[i];
        ymax4[i] = dataC->maxData4[i];
        ymax5[i] = dataC->maxData5[i];
        ymax6[i] = dataC->maxData6[i];
        ymax7[i] = dataC->maxData7[i];
        ymax8[i] = dataC->maxData8[i];
        ymax9[i] = dataC->maxData9[i];
        ymaxA[i] = dataC->maxDataA[i];
        ymaxB[i] = dataC->maxDataB[i];
    }
    ui->widget->graph(0)->setData(xx, ymax0);
    ui->widget->graph(1)->setData(xx, ymax1);
    ui->widget->graph(2)->setData(xx, ymax2);
    ui->widget->graph(3)->setData(xx, ymax3);
    ui->widget->graph(4)->setData(xx, ymax4);
    ui->widget->graph(5)->setData(xx, ymax5);
    ui->widget->graph(6)->setData(xx, ymax6);
    ui->widget->graph(7)->setData(xx, ymax7);
    ui->widget->graph(8)->setData(xx, ymax8);
    ui->widget->graph(9)->setData(xx, ymax9);
    ui->widget->graph(10)->setData(xx, ymaxA);
    ui->widget->graph(11)->setData(xx, ymaxB);
}

void MainWindow::findPlot(){
    dataC->findMaxMinData(minX, maxX, flag);
    mD2QVector();
    ui->widget->replot();
}

void MainWindow::homeWaveForm(){
    maxX = dataC->index0;
    minX = 0;
    minY = 0;
    maxY = 4096;
    dataC->findMaxMinData(minX, maxX, flag);
    qDebug()<<"find max/min/ind over";

    mD2QVector();

    ui->widget->graph(0)->rescaleAxes();
    ui->widget->yAxis->setRange(minY, maxY);
    ui->widget->replot();
}

void MainWindow::waveForm(){
    QString str = QFileDialog::getOpenFileName(this,"打开文件", "C:/Users/62320/Desktop", "(*.txt)");
    qDebug()<<str;
    dataC->txt2dataV(str.toUtf8().data());
    qDebug()<<"txt to dataVector over";
    homeWaveForm();
    QLabel * label = new QLabel("文件路径："+str,this);
    ui->statusBar->addWidget(label);
}

void MainWindow::resetRange(){
    float minmin;
    if((end_x_val-begin_x_val)<2&(end_x_val-begin_x_val)>-2){
        QMessageBox::warning(this, "warning", "警告:您刚刚横向框选距离太小");
    }else{
        if((end_y_val-begin_y_val)<2&(end_y_val-begin_y_val)>-2){
            QMessageBox::warning(this, "warning", "警告:您刚刚纵向框选距离太小");
        }else{
            if(begin_x_val<end_x_val){
                minmin = begin_x_val;
            }else{
                minmin = end_x_val;
            }

            if(minmin<0){
                QMessageBox::warning(this, "warning", "警告:您刚刚框选了Y轴左侧区域");
            }else{
                if(begin_x_val<end_x_val){
                    minX = begin_x_val;
                    maxX = end_x_val;
                }else{
                    minX = end_x_val;
                    maxX = begin_x_val;
                }
                if(begin_y_val<end_y_val){
                    minY = begin_y_val;
                    maxY = end_y_val;
                }else{
                    minY = end_y_val;
                    maxY = begin_y_val;
                }

                dataC->findMaxMinData(minX, maxX, flag);

                mD2QVector();

                ui->widget->xAxis->setRange(minX, maxX);
                ui->widget->yAxis->setRange(minY, maxY);
                ui->widget->replot();
            }
        }
    }
}

void MainWindow::resetPos(){
    //···
    float changeX,changeY;
    float minmin=minX;
    changeX = end_x_val-begin_x_val;
    changeY = end_y_val-begin_y_val;

    minmin -= changeX;

    if(minmin<0){
        //
        maxX -= minX;
        minX = 0;
        QMessageBox::warning(this, "warning", "警告：您已向右拖动到了极限");
    }else{
        minX -= changeX;
        maxX -= changeX;
    }
    minY -= changeY;
    maxY -= changeY;

    dataC->findMaxMinData(minX, maxX, flag);

    mD2QVector();

    ui->widget->xAxis->setRange(minX, maxX);
    ui->widget->yAxis->setRange(minY, maxY);
    ui->widget->replot();
}

void MainWindow::mousePressEvent(QMouseEvent *event){
    int x_pos = event->pos().x();
    int y_pos = event->pos().y();

    // 把鼠标坐标点 转换为 QCustomPlot 内部坐标值 （pixelToCoord 函数）
    // coordToPixel 函数与之相反 是把内部坐标值 转换为外部坐标点
    begin_x_val = ui->widget->xAxis->pixelToCoord(x_pos);
    begin_y_val = ui->widget->yAxis->pixelToCoord(y_pos);
    //这里在读取y轴坐标的时候会有误差，做一个补偿
    //begin_y_val += 120;
    qDebug()<<"BWX="<<x_pos<<";BWY="<<y_pos<<"***BPX="<<begin_x_val<<";BPY="<<begin_y_val;

}

void MainWindow::mouseReleaseEvent(QMouseEvent *event){
    int x_pos = event->pos().x();
    int y_pos = event->pos().y();

    // 把鼠标坐标点 转换为 QCustomPlot 内部坐标值 （pixelToCoord 函数）
    // coordToPixel 函数与之相反 是把内部坐标值 转换为外部坐标点
    end_x_val = ui->widget->xAxis->pixelToCoord(x_pos);
    end_y_val = ui->widget->yAxis->pixelToCoord(y_pos);
    //这里在读取y轴坐标的时候会有误差，做一个补偿
    //end_y_val += 120;
    qDebug()<<"EWX="<<x_pos<<";EWY="<<y_pos<<"***EPX="<<end_x_val<<";EPY="<<end_y_val;

    if(event->button() == Qt::RightButton){
        resetRange();
    }else if (event->button() == Qt::LeftButton) {
        resetPos();
    }
}

MainWindow::~MainWindow(){
    delete ui;
}



//目前出现的问题：当点击Qcustomplot下面的区域的时候，也会重新框选。
//拟采用的解决方案：将配置文件采用非模态对话框的方式弹出，然后进行配置

//目前出现的问题：当使用右键时，依旧会框选，并出现bug。
//现在的解决方案：当使用右键时，是框选功能
//当使用左键时，是拖动波形功能

//目前要求的功能：鼠标可以拖动波形
//拟采用的解决方案：鼠标左键可以上下左右拖动？？？？
//这里需要注意一个问题，如果是上下拖动，只需要更改range即可
//但是如果是左右拖动，需要重新选点！！！
//如果是可以同时上下左右拖动呢？？？？
